package com.leju.common.lrucache;


import java.io.File;
import java.util.HashMap;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalBitmap.DownLoadBitmapCallBack;
import net.tsz.afinal.bitmap.core.BitmapDisplayConfig;
import net.tsz.afinal.bitmap.core.BitmapDisplayConfig.AnimationType;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

public class ImageViewAsyncLoadingTask
{


	private FinalBitmap mFinalBitmap;

	public static Context context;

	public static String filePath;
	private final int THREAD_SIZE = 7;
	
	private static BitmapDisplayConfig config;
	
	private static HashMap<String, Bitmap>  bitmapMap;
	
	public ImageViewAsyncLoadingTask()
	{
		//TODO 加上非空判断
		if(context != null)
			mFinalBitmap = FinalBitmap.create(context, THREAD_SIZE);
		if(null == config){
			config = new BitmapDisplayConfig();
			config.setAnimationType(AnimationType.fadeIn);
		}
		if(null == bitmapMap) bitmapMap = new HashMap<String, Bitmap>();
	}
	
	public void execute(final ImageView imageView, final String imageUrl)
	{
		doExecute(imageView, imageUrl, null, -1, false, null);
	}
	
	public void execute(final ImageView imageView, final String imageUrl, int defaultDrawableResId){
		doExecute(imageView, imageUrl, null, defaultDrawableResId, false, null);
	}
	
	public void execute(final ImageView imageView, final String imageUrl, String name, int defaultDrawableResId){
		
		doExecute(imageView, imageUrl, name, defaultDrawableResId, false, null);
	}
	
	/**
	 * 
	 * @param imageView 显示图片的Imageview
	 * @param imageUrl url地址
	 * @param defaultDrawableResId  默认的图片id
	 * @param neverCalculate 是否缩放图片质量  true不缩放，false缩放
	 */
	public void execute(final ImageView imageView, final String imageUrl, int defaultDrawableResId, boolean neverCalculate){
		doExecute(imageView, imageUrl, null, defaultDrawableResId, neverCalculate, null);
	}
	
	/**
	 * 
	 * @param imageView 显示图片的Imageview
	 * @param imageUrl url地址
	 * @param defaultDrawableResId  默认的图片id
	 * @param neverCalculate 是否缩放图片质量  true不缩放，false缩放
	 * @param callBack 下载图片过程中的回调接口
	 */
	public void execute(final ImageView imageView, 
			final String imageUrl, 
			int defaultDrawableResId, 
			boolean neverCalculate, 
			DownLoadBitmapCallBack callBack){
		
		doExecute(imageView, imageUrl, null, defaultDrawableResId, neverCalculate, callBack);
	}
	
	/**
	 * 
	 * @param imageView 显示图片的Imageview
	 * @param imageUrl url地址
	 * @param defaultDrawableResId  默认的图片id
	 * @param neverCalculate 是否缩放图片质量  true不缩放，false缩放
	 * @param callBack 下载图片过程中的回调接口
	 */
	public void execute(final ImageView imageView, final String imageUrl, DownLoadBitmapCallBack callBack)
	{
		doExecute(imageView, imageUrl, null, -1, false, callBack);
	}
	
	private void doExecute(final ImageView imageView, 
			final String imageUrl, 
			String name, 
			int defaultDrawableResId, 
			boolean neverCalculate, 
			DownLoadBitmapCallBack callBack){
		
//		neverCalculate = true;//true不缩放，false缩放
		
		if(defaultDrawableResId > 0 || neverCalculate){
			if (defaultDrawableResId > 0) {
				if (imageView != null) {
					imageView.setImageResource(defaultDrawableResId);
				}
				
				if(null == bitmapMap) bitmapMap = new HashMap<String, Bitmap>();
				if(null == bitmapMap.get("" + defaultDrawableResId)) 
					bitmapMap.put(String.valueOf(defaultDrawableResId), 
						BitmapFactory.decodeResource(context.getResources(), defaultDrawableResId));
				
				config.setLoadingBitmap(bitmapMap.get(String.valueOf(defaultDrawableResId)));
			}
			
			config.setNeverCalculate(neverCalculate);
			//TODO mFinalBitmap对象加上非空判断
			if(mFinalBitmap != null){
				mFinalBitmap.display(imageView, imageUrl, config, callBack);
			}
		}else{
			//TODO mFinalBitmap对象加上非空判断
			if(mFinalBitmap != null){
				mFinalBitmap.display(imageView, imageUrl, callBack);
			}
		}
	}
	
	public static void init(Context ctx){
		context = ctx;
		filePath = "/Android/data/" + context.getPackageName() + File.separator + "files" + File.separator;
	}
	
	
}